
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Security;

using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Core;

namespace Localization
{
	public class LocalizationBackupRestoreBizRules : Syrinx.BaseBizRules
	{
		public void backupTranslations(IApplication app, IRuleContext context, IBizObj deployInfo, ZipOutputStream zOut)
		{
		}

		public void restoreTranslations(IApplication app, IRuleContext context, IBizObj bkInfo, ZipFile zipFile)
		{
		}

		public void postExtendLocalizationExecuteCompanyRestore(IApplication app, IRuleContext context, object returnValue, object[] inputParams)
		{
			try
			{
				INameAddressSupport deployInfo = inputParams[2] as INameAddressSupport;
				ZipFile zipFile = inputParams[3] as ZipFile;
				IResults rc = null;
				rc = context.RuleGroup.execute(context, "restoreTranslations", deployInfo, zipFile);
				if (rc.ExceptionInfo != null)
					throw rc.ExceptionInfo;
			}
			catch (Exception e)
			{
				throw new BaseException("error executing postExtendEComexecuteCompanyRestore.", e);
			}
		}

		public void postExtendLocalizationExecuteCompanyBackup(IApplication app, IRuleContext context, object returnValue, object[] inputParams)
		{

			try
			{
				INameAddressSupport deployInfo = inputParams[2] as INameAddressSupport;
				ZipOutputStream zOut = inputParams[3] as ZipOutputStream;
				IResults rc = null;
				bool fullBackup = (bool)deployInfo.get("fullCompanyBackup");

				if (fullBackup || (bool)deployInfo.get("backupTranslations"))
					rc = context.RuleGroup.execute(context, "backupTranslations", deployInfo, zOut);
			}
			catch (Exception e)
			{
				throw new BaseException("error executing postExtendEComexecuteCompanyBackup.", e);
			}
		}

		public object getLocalizationBackupRestoreTemplate(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			string template = "Localization.Gui.AspNet.BaseDeploymentOptions";
			if (resultValue != null)
				return resultValue.ToString().Length == 0 ? template
					: new StringBuilder(50).Append(resultValue.ToString()).Append(",").Append(template).ToString();
			return template;
		}


	}
}
